package org.example.pmsystem.service.Impl;

import org.example.pmsystem.entity.Interview;
import org.example.pmsystem.entity.Resume;
import org.example.pmsystem.mapper.InterviewMapper;
import org.example.pmsystem.mapper.ResumeMapper;
import org.example.pmsystem.service.InterviewService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class InterviewServiceImpl  implements InterviewService {
    @Autowired
    InterviewMapper interviewMapper;
    @Autowired
    ResumeMapper resumeMapper;

    @Override
    public List<Interview> getAllInterview() {
        return interviewMapper.getAllInterview();
    }

    @Override
    public List<Interview> selectInterviewByPeriod(String interPeriod) {
        return interviewMapper.selectInterviewByPeriod(interPeriod);
    }

    @Override
    public List<Interview> selectInterviewByStatus(String interStatus) {
        return interviewMapper.selectInterviewByStatus(interStatus);
    }

    @Override
    public List<Interview> selectInterviewByDep(String dep){
        return interviewMapper.selectInterviewByDep(dep);
    }

//    private Interview modify(Interview interview){
//        switch (interview.getInterPeriod()){
//            case "1":
//                interview.setInterPeriod("一面");break;
//            case "2":
//                interview.setInterPeriod("二面");break;
//            case "3":
//                interview.setInterPeriod("三面");break;
//        }
//
//        switch (interview.getInterStatus()){
//            case "1":
//                interview.setInterStatus("待面试");break;
//            case "2":
//                interview.setInterStatus("未通过");break;
//            case "3":
//                interview.setInterStatus("已通过面试");break;
//            case "4":
//                interview.setInterStatus("已发录用函");break;
//        }
//        return interview;
//    }

    @Override
    public boolean insertInterview(Interview interview) {
        // 修改简历状态
        Resume resume = resumeMapper.selectResumeByResNo(interview.getResNo());
        resume.setResStatus("8");
        resumeMapper.updateResume(resume);

        // 插入面试信息
        LocalDateTime time = interview.getInterTime();
        if (time!=null && time.isBefore(LocalDateTime.now()))
            return false;
//        interview = modify(interview);
        interview.setInterStatus("1");
        interview.setInterPeriod("1");
        interviewMapper.insertInterview(interview);
        return true;
    }

    @Override
    public boolean updateInterview(Interview interview) {
        LocalDateTime time = interview.getInterTime();
        if (time!=null && time.isBefore(LocalDateTime.now()))
            return false;
        Interview temp = interviewMapper.selectInterviewByinterNo(interview.getInterNo());
        String period = temp.getInterPeriod();
        if (!period.equals(interview.getInterPeriod())
                && (period.equals("1") || period.equals("2")))
            interview.setInterStatus("1");
//        interview = modify(interview);
        interviewMapper.updateInterview(interview);
        return true;
    }

    @Override
    public void deleteInterviewByNo(int interNo){
        interviewMapper.deleteInterviewByNo(interNo);
    }
}
